<?php
/**
 * Created by IntelliJ IDEA.
 * User: sunminqiang
 * Date: 17/3/8
 * Time: 上午10:28
 */
namespace app\admin\controller;
use app\admin\model\TrademarkModel;
use think\Db;

class Trademark extends Base
{

    /**
     * [index 文章列表]
     * @author [田建龙] [864491238@qq.com]
     */
    public function index(){

        $key = input('key');
        $map = [];
        if($key&&$key!==""){
            $map['title'] = ['like',"%" . $key . "%"];
        }
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = 10;// 获取总条数
        $count = Db::name('auditing')->where($map)->count();//计算总页面
        $allpage = intval(ceil($count / $limits));
        $trademark = new TrademarkModel();
        $lists = $trademark->getTrademarkByWhere($map, $Nowpage, $limits);
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('count', $count);
        $this->assign('val', $key);
        if(input('get.page')){
            return json($lists);
        }
        return $this->fetch();
    }


    /**
     * [add_article 添加文章]
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function add_trademark()
    {

        if(request()->isAjax()) {

            $param = input('post.');
            $registration = $param['registration'];
            $repeat = Db::query('select * from think_auditing where registration = ?', [$registration]);

            if (!empty($repeat)) {
                return json(['code' => 4]);
            } else {
                $trademark = new TrademarkModel();
                $flag = $trademark->insertTrademark($param);
                return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
            }
        }
        $cate = Db::query('select * from think_category ');
        $this->assign('cate',$cate);
        return $this->fetch();
    }


    /**
     * [edit_article 编辑文章]
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function edit_trademark()
    {
        $trademark = new TrademarkModel();

        if(request()->isAjax()){

            $param = input('post.');

            $flag = $trademark->updateTrademark($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $id = input('param.id');
        $cate = Db::query('select * from think_category ');
        $this->assign('cate',$cate);
        $this->assign('trademark',$trademark->getOneTrademark($id));
        return $this->fetch();
    }



    /**
     * [del_article 删除文章]
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function del_trademark()
    {
        $id = input('param.id');
        $cate = new TrademarkModel();
        $flag = $cate->delTrademark($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }



    /**
     * [article_state 文章状态]
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function trademark_shows()
    {
        $id=input('param.id');
        $status = Db::name('auditing')->where(array('id'=>$id))->value('shows');//判断当前状态情况
        if($status==1)
        {
            $flag = Db::name('auditing')->where(array('id'=>$id))->setField(['shows'=>0]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已禁止']);
        }
        else
        {
            $flag = Db::name('auditing')->where(array('id'=>$id))->setField(['shows'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已开启']);
        }

    }



    //*********************************************分类管理*********************************************//

    /**
     * [index_cate 分类列表]
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function index_cate(){

        $cate = new TrademarkCateModel();
        $list = $cate->getAllCate();
        $this->assign('list',$list);
        return $this->fetch();
    }


    /**
     * [add_cate 添加分类]
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function add_cate()
    {
        if(request()->isAjax()){

            $param = input('post.');
            $cate = new TrademarkCateModel();
            $flag = $cate->insertCate($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        return $this->fetch();
    }


    /**
     * [edit_cate 编辑分类]
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function edit_cate()
    {
        $cate = new TrademarkCateModel();

        if(request()->isAjax()){

            $param = input('post.');
            $flag = $cate->editCate($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $id = input('param.id');
        $this->assign('cate',$cate->getOneCate($id));
        return $this->fetch();
    }


    /**
     * [del_cate 删除分类]
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function del_cate()
    {
        $id = input('param.id');
        $cate = new TrademarkCateModel();
        $flag = $cate->delCate($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }



    /**
     * [cate_state 分类状态]
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function cate_state()
    {
        $id=input('param.id');
        $status = Db::name('category')->where(array('id'=>$id))->value('shows');//判断当前状态情况
        if($status==1)
        {
            $flag = Db::name('category')->where(array('id'=>$id))->setField(['shows'=>0]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已禁止']);
        }
        else
        {
            $flag = Db::name('category')->where(array('id'=>$id))->setField(['shows'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已开启']);
        }

    }

}